﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Cashier_Interface;
using Cashier.utils;
using Cashier_Model;
using Cashier_Service;
using Cashier_Utils;

namespace Cashier.orgs
{
    public partial class StoreInfoListControl : UserControl, IDataGridViewData, IReloadData
    {
        private MainForm mainform;
        private static StoreInfoListControl storeInfoListControl;
        private StoreInfoService storeInfoService;

        /// <summary>
        /// 构造函数
        /// </summary>
        private StoreInfoListControl(MainForm mainform)
        {
            InitializeComponent();
            this.dataGridView1.ImplementsInstance = this;
            this.mainform = mainform;
            this.storeInfoService = new StoreInfoService();

            this.ControlsSetRights();
            this.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 返回StoreInfoListControl实例
        /// </summary>
        /// <param name="mainform"></param>
        /// <returns></returns>
        public static StoreInfoListControl GetStoreInfoListControl(MainForm mainform)
        {
            if (storeInfoListControl == null)
                storeInfoListControl = new StoreInfoListControl(mainform);
            storeInfoListControl.RefreshDataGridViewData();
            return storeInfoListControl;
        }

        #region 功能实现

        /// <summary>
        /// 控件设置权限
        /// </summary>
        private void ControlsSetRights()
        {
            Button[] buttons = { button_query, button_add };
            RightsforControls.RightsforButton(mainform.Admin.RolePurviewtable, buttons);
            ContextMenuStrip[] menuStrips = { contextMenuStrip_dgv };
            RightsforControls.RightsforContextMenuStrip(mainform.Admin.RolePurviewtable, menuStrips);
        }

        /// <summary>
        /// 打开保存窗口
        /// </summary>
        protected void OpenSaveForm(Int64 storeInfoId, String storeNo)
        {
            StoreInfoAddForm storeInfoAddForm = new StoreInfoAddForm(mainform, this, storeInfoId, storeNo);
            storeInfoAddForm.ShowDialog();
        }

        #endregion

        #region IDataGridViewData 成员

        void IDataGridViewData.EnterkeyEventHandler()
        {
            return;
        }

        void IDataGridViewData.GenerateGridViewColumns(DataGridView dgv)
        {
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.Columns.Clear();
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("id", null, true, 0, null, null, false, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("store_no", "门店号", true, 9, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("name", "名称", true, 10, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("contact", "联系人", true, 9, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("mobile", "手机", true, 9, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("tel", "电话", true, 9, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("fax", "传真", true, 9, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("fax", "邮箱", true, 9, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("address", "地址", true, 9, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("zip", "邮编", true, 9, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("note", "备注", true, 9, ActKey.LEFT, ActKey.LEFT, true, false));
            this.dataGridView1.Columns.Add(GenearteDataGridViewColumn.GetDataGridViewDataFieldColumn("create_time", "创建日期", true, 9, ActKey.CENTER, ActKey.CENTER, true, false));
        }

        void IDataGridViewData.GetBindingData(int rowindex, int pageindex, int pagesize)
        {
            try
            {
                String name = textbox_name.Text.Trim();
                StoreInfo storeInfo = new StoreInfo();
                storeInfo.Name = name;
                storeInfo.Pageindex = pageindex;
                storeInfo.Pagesize = pagesize;
                storeInfo.Ascfield = "id";
                DataTable dt = this.storeInfoService.GetStoreInfoTable(storeInfo);
                this.dataGridView1.DataSource = dt;
                if (dt != null && dt.Rows.Count > 0)
                {
                    if (rowindex > 0)
                        dataGridView1.Rows[0].Selected = false;
                    dataGridView1.Rows[rowindex].Selected = true;
                }
                dataGridView1.Columns[0].Visible = false;

                mainform.SetPageInfo(dataGridView1.Recordcount, pageindex, pagesize);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        long IDataGridViewData.GetBindingDataRecord()
        {
            long record = 0;
            try
            {
                String name = textbox_name.Text.Trim();
                StoreInfo storeInfo = new StoreInfo();
                storeInfo.Name = name;
                record = this.storeInfoService.GetStoreInfoRecord(storeInfo);
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
            return record;
        }

        #endregion

        #region 刷新数据

        public void RefreshDataGridViewData()
        {
            this.dataGridView1.LoadingData();
        }

        #endregion

        #region 重写事件

        /// <summary>
        /// 重写键盘事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="keyData"></param>
        /// <returns></returns>
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            bool b = base.ProcessCmdKey(ref msg, keyData);
            switch (keyData)
            {
                case Keys.Up:
                    this.dataGridView1.Focus();
                    break;
                case Keys.Down:
                    this.dataGridView1.Focus();
                    break;
                case Keys.F8:
                    this.RefreshDataGridViewData();
                    break;
                case Keys.F9:
                    this.OpenSaveForm(0, null);
                    break;
            }
            return b;
        }

        #endregion

        #region IReloadData 成员

        void IReloadData.ReloadDataGridViewData(object param)
        {
            this.RefreshDataGridViewData();
        }

        #endregion

        #region 控件事件

        /// <summary>
        /// DataGridView 单元格双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                Int64 storeInfoId = (long)this.dataGridView1.Rows[e.RowIndex].Cells[0].Value;
                String storeNo = this.dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
                this.OpenSaveForm(storeInfoId, storeNo);
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_query_Click(object sender, EventArgs e)
        {
            this.RefreshDataGridViewData();
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_add_Click(object sender, EventArgs e)
        {
            this.OpenSaveForm(0, String.Empty);
        }

        #endregion

        #region DataGridView右键菜单

        /// <summary>
        /// 新增门店
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_add_Click(object sender, EventArgs e)
        {
            this.OpenSaveForm(0, String.Empty);
        }

        /// <summary>
        /// 编辑门店
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_edit_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
                {
                    Int64 storeInfoId = (long)this.dataGridView1.SelectedRows[0].Cells[0].Value;
                    String storeNo = this.dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                    this.OpenSaveForm(storeInfoId, storeNo);
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 删除门店
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_delete_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
                {
                    DialogResult result = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo);
                    if (result == DialogResult.Yes)
                    {
                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
                        {
                            long memberid = (long)dataGridView1.SelectedRows[i].Cells[0].Value;
                            sb.Append(memberid).Append(",");
                        }
                        if (sb.Length > 0)
                        {
                            sb.Remove(sb.Length - 1, 1);
                            int count = storeInfoService.DeleteStoreInfo(sb.ToString());
                            if (count > 0)
                            {
                                MessageBox.Show("删除成功！");
                                this.RefreshDataGridViewData();
                            }
                            else
                                MessageBox.Show("删除失败！");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        /// <summary>
        /// 复印门店号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripMenuItem_copyStoreNo_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count > 0)
                {
                    String storeNo = this.dataGridView1.Rows[0].Cells[1].Value.ToString();
                    Clipboard.SetText(storeNo);
                }
            }
            catch (Exception ex)
            {
                LogsUtil.Record(ex);
            }
        }

        #endregion

    }
}
